package com.googlecode.objectify.test.dataloader;

import org.milyn.scribe.annotation.Dao;
import org.milyn.scribe.annotation.Insert;

import com.google.appengine.api.datastore.Transaction;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.util.DAOBase;

/**
 * Smooks DAO for Objectify to persist and create Keys.
 * @author Shakil Siraj
 */
@Dao
public class ObjectifySmooksDAO {

    /**
     * Reference to {@link DAOBase}.
     */
    private final DAOBase daoBase;

    /**
     * Constructor.
     * @param daoBase
     *            The Objectify {@link DAOBase}
     */
    public ObjectifySmooksDAO(final DAOBase daoBase) {
	this.daoBase = daoBase;
    }

    /**
     * Insert the entity into Objectify store.
     * @param entity
     *            The entity to be stored
     */
    @Insert
    public final void insertEntity(final Object entity) {
	if (entity != null) {
	    Objectify ofy = daoBase.fact().beginTransaction();
	    Transaction tx = ofy.getTxn();
	    ofy.put(entity);
	    tx.commit();
	}
    }
}
